import { getCategoryApi } from '@/api/home/goodsCategory';
import { useRoute, onBeforeRouteUpdate } from 'vue-router';
import { ref, onMounted } from 'vue';

export function useCategory() {
  const categoryData = ref<any>({});
  //获取页面id
  const route = useRoute();
  //获取分类
  const getCategory = async (id = route.params.id) => {
    const { data } = await getCategoryApi(id);
    console.log(data);
    categoryData.value = data;
  };
  /*监听路由变化*/
  onBeforeRouteUpdate(async (to) => {
    await getCategory(to.params.id);
  });

  onMounted(async () => {
    await getCategory();
  });
  return {
    categoryData
  };
}
